<?PHP

class paginator
{
	var $data;
	var $items=25;
	var $page=2;
	var $count=0;
	var $pd;
	var $q;
	var $cmd;
	var $pager;
	var $pages;
	var $first;
	var $last;
	var $cleanurl;
	
	var $padding=5;
	
	
	
	function paginate()
	{
		
		global $conf;
		$this->page=$conf->page;
		//$this->items=$conf->a['items-per-page'];

		$this->count=count($this->data);
		mp('count:'.$this->count);
		$start_count=($this->items * $this->page )-$this->items +1;
		$this->pages=ceil($this->count/$this->items);
		$end_count=$this->items* $this->page;
		mp('start:'.$start_count);
		mp('end:'.$end_count);
		$item_counter=0;
		foreach ($this->data as $post_key => $x)
		{
			
			//echo $this->count;
			$item_counter++;
			if($item_counter>=$start_count && $item_counter<=$end_count)
			{
			
				$m[$post_key]=$x;
			}
			
		}
		$this->make_pager();
		$this->pd=$m;
		return $m;
	}
	
	function make_pager()
	{
		mp('Run Pager');
		mp($this->pages);
		global $conf;
		$script_name=$script_name;
		if($this->cleanurl)
		{
			$queries=get_clean_url_queries();
			$script_name=$conf->path;
		}
		else
		{
			$queries=get_clean_queries();
			$script_name=$_SERVER['SCRIPT_NAME'];
		}
		
		mp($queries);
		$pages=$this->pages-1;
		$pager['First']=$pager[$x]='http://'.$_SERVER['HTTP_HOST'].$script_name.$queries.'&page=1';
	
		
		// If our current page, minus our padding, is greater than 1
		if(($this->page - $this->padding) > 1) {

			// Set our lower limit
			$lowerLimit = $this->page - $this->padding;
			// Print all padded numbers between lowerLimit and current page
			for($i = $lowerLimit; $i < $this->page; $i++) {
					$pager[$i]='http://'.$_SERVER['HTTP_HOST'].$script_name.$queries.'&page='.$i;
			}
		} else {
			// Print all numbers between current page, and first page
			for($i = 2; $i < $this->page; $i++) {
				$pager[$i]='http://'.$_SERVER['HTTP_HOST'].$script_name.$queries.'&page='.$i;
			}
		}

		// If we're not on the first page, or the last page, print current page
		if(($this->page != 0) && ($this->page != 1) && ($this->page != $this->pages)) {

				$pager[$this->page]='';
			
		}

		// If our current page, plus our padding, is less than the total number of pages
		if(($this->page + $this->padding) < $this->pages) {
			// Set upper limit
			$upperLimit = $this->page + $this->padding;
			// Print all number from padded pages above current page
			for($i = ($this->page + 1); $i <= $upperLimit; $i++) {
					$pager[$i]='http://'.$_SERVER['HTTP_HOST'].$script_name.$queries.'&page='.$i;
			}
	
		} else {
			// Print all page numbers between number of pages and current page
			for($i = ($this->page + 1); $i < $this->pages; $i++) {
						$pager[$i]='http://'.$_SERVER['HTTP_HOST'].$script_name.$queries.'&page='.$i;
			}
		}
		$pager['Last']=$pager[$x]='http://'.$_SERVER['HTTP_HOST'].$script_name.$queries.'&page='.$this->pages;
		
		
		
		
			
		$this->pager=$pager;
	}
	
	function print_pager()
	{
			?>
			<ul class='paginator'>
				<?PHP
				foreach ($this->pager as $post_key => $x)
				{
		
							global $conf;
							if($post_key == '')
							{
							
									
							}
							else if($x=='')
							{
										?>
											<li class='paginator-current'><?PHP  echo $post_key;?></li>
										<?PHP
							}
							else
							{
							?>
								<li><a href="<?PHP echo $x;?>"><?PHP  echo $post_key;?></a></li>
							<?PHP
							}
					
		
				}
					?>
			</ul>
			<?PHP
	}
}
?>